const bodyParser = require('body-parser');
const { Router, query } = require('express')
let router = new Router()

const petModel = require('../models/petModel')

// 获取宠物详细信息
router.get('/petInfo', async (req, res) => {
    try {
        const { aid, id, kind, type, emil, currentPage, pageSize,isExist } = req.query;
        let params = {};
        let result = {};
        if (currentPage && pageSize) {
            let pageNumber = (currentPage - 1) * (+pageSize);
            if (aid)
                params.belong = aid;
            if (type)
                params.type = type;
            result.data = await petModel.find(params).skip(pageNumber).limit(+pageSize);
            result.total = await petModel.find(params).countDocuments();
        } else {
            if (type)
                params.type = type;
            if (aid)
                params.belong = aid;
            if (kind)
                params.kind = kind;
            if (id)
                params.id = id;
            if (emil)
                params.mail = mail;
            if(isExist == 'false'){
              params.isExist = false
            }
            result = await petModel.find(params);
        }
        res.send({
            success: true,
            data: result
        })
    } catch (e) {
        res.send({
            success: false,
            data: [],
            msg: e
        })
    }
})

router.get('/filterList', async (req, res) => {
    const { type, kind, agelist, size, gender } = req.query
    let dataInfo = {}
    if (type) dataInfo.type = type
    if (kind) dataInfo.kind = kind
    if (size) dataInfo.size = size
    if (gender) dataInfo.gender = gender
    if (agelist) {
        switch (agelist) {
            case 'baby': dataInfo.age = { $lte: 1 }; break;
            case 'young': dataInfo.age = { $gt: 1, $lte: 2 }; break;
            case 'adult': dataInfo.age = { $gt: 2, $lte: 8 }; break;
            case 'senior': dataInfo.age = { $gt: 8 }; break;
        }
    }
    let typeInfo = await petModel.find(dataInfo)
    if (typeInfo) {
        res.send({ success: true, data: typeInfo })
    } else {
        res.send({ success: false, message: '查询失败' })
    }

})

// 新增宠物
router.post('/pet/add', async (req, res) => {
    try {
        let id = new Date().getTime();
        let result = await petModel.create({
            ...req.body,
            id
        })
        res.send({
            success: true,
            data: result
        })
    } catch (e) {
        res.send({
            success: false,
            msg: 'false' + e
        })
    }
})

// 更新宠物
router.post('/pet/update', async (req, res) => {
    try {
        let body = req.body;
        let params = {};
        if (body.pname)
            params.pname = body.pname;
        if (body.kind)
            params.kind = body.kind;
        if (body.size)
            params.size = body.size;
        if (body.gender)
            params.gender = body.gender;
        if (body.age)
            params.age = body.age;
        if (body.details)
            params.details = body.details;
        if (body.profile)
            params.profile = body.profile;
        if (body.type)
            params.type = body.type;
        if (body.height)
            params.height = body.height;
        if (body.weight)
            params.weight = body.weight;
        if (body.color)
            params.color = body.color;
        if ((typeof body.isExist) === 'boolean')
            params.isExist = body.isExist;
        if (body.charact)
            params.charact = body.charact;
        if (body.reason)
            params.reason = body.reason;
        let result = await petModel.updateOne(
            {
                id: body.id
            },
            params
        )
        res.send({
            success: true,
            data: result
        })
    } catch (e) {
        res.send({
            success: false,
            msg: 'false' + e
        })
    }
})

// 删除宠物
router.get('/pet/delete', async (req, res) => {
    try {
        let { id } = req.query;
        let result = await petModel.deleteOne({
            id
        })
        res.send({
            success: true,
            data: result
        })
    } catch (e) {
        res.send({
            success: false,
            msg: 'false' + e
        })
    }
})

module.exports = router